home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_060 / ueturbo / check1.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  89 lines

  1. /****************************************************************
  2. *                                                               *
  3. *         CC (C Checker)                                        *
  4. *                                                               *
  5. *        C Source Paren, bracket and comment Checker            *
  6. *                                                               *
  7. *            T. Jennings  -- Sometime in 1983                   *
  8. *                                                               *
  9. *          Ported to Amiga 3/30/86 by T. Althoff                *
  10. *                                                               *
  11. *   (Renamed 'check' to avoid conflict with cc unix command)    *
  12. *                                                               *
  13. ****************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17.  
  18. /* Very crude but very effective C source debugger. Counts the numbers of
  19. matching braces, parenthesis and comments, and displays them at the left edge
  20. of the screen. The best way to see what it does is to do it; try
  21.  
  22. >check check.c
  23.  
  24. Properly handles parens and brackets inside comments; they are ignored.
  25. */
  26.  
  27.  
  28.  
  29. main(argc,argv)
  30.  
  31. int argc;
  32. char **argv;
  33. {
  34. int file;   
  35. char c,lastc;
  36. int parens,brackets,comments;
  37. int line, col;
  38. char hdr[40];
  39.  
  40.    file= open(argv[1],0x8000);
  41.    if (file == -1) {
  42.       printf("File missing. Try check <filename.ext> \r\n");
  43.       exit();
  44.    }
  45.  
  46.    brackets= parens= comments= 0;
  47.    line= 0; col= 0;
  48.    lastc= '\0';
  49.  
  50.    while (read(file,&c,1)) {
  51.       if (c == 0x1a) break;         /* stop if ^Z */
  52.       if (col == 0) {
  53.          sprintf(hdr,"%d: {%d} (%d) /*%d*/",line,brackets,parens,comments);
  54.          while (strlen(hdr) < 15)   /* gross but works */
  55.             strcat(hdr," ");   /* to hell with elegant, */
  56.          printf("%s|",hdr);      /* we got a job to do !*/
  57.       }            /* (real programmers dont ...) */
  58.  
  59. /* Dont count parens and brackets that are inside comments. This of course
  60. assumes that comments are properly matched; in any case, that will be the
  61. first thing to look for. */
  62.  
  63.       if (comments <= 0) {
  64.          if (c == '{') ++brackets;
  65.          if (c == '(') ++parens;
  66.          if (c == '}') --brackets;
  67.          if (c == ')') --parens
  68.       }
  69.  
  70. /* Now do comments. This properly handles nested comments, whether or
  71. not the compiler does is your responsibility */
  72.  
  73.      if ((c == '*') && (lastc == '/')) ++comments;
  74.       if ((c == '/') && (lastc == '*')) --comments;
  75.  
  76.       ++col;
  77.       if (c == 0x0a) {      /* newline == New Line */
  78.          col= 0;         /* set column 0 */
  79.          ++line;
  80.       }
  81.       putchar(c);         /* display text */
  82.       lastc= c;         /* update last char */
  83.    }
  84.    printf("\r\n\r\n");
  85.    if (brackets) printf("Unbalanced brackets\r\n");
  86.    if (parens) printf("Unbalanced parenthesis\r\n");
  87.    if (comments) printf("Unbalanced comments\r\n");
  88. }
  89.